package org.biodas.jdas.creators;

import org.biodas.jdas.schema.features.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
/**
 * Helper class for generating Features and Features response representations either for mock objects or to generate xml/json responses
 * There is no checking in this class for valid values as it can't then be used as JUnit mock objects - need to validate using other classes.
 * LPGL
 *@author JWarren
 *
 */
public class CreateFeatures {
	ObjectFactory factory=new ObjectFactory();
	
	public PART createPart(String id) {
		PART part=factory.createPART();
		part.setId(id);
		return part;
	}
	
	public PARENT createParent(String id) {
		PARENT parent=factory.createPARENT();
		parent.setId(id);
		return parent;
	}
	/**
	 * Should use parent parts instead of group- group is deleted in the DAS 1.6 spec, but this is provided here in case your client only supports old 1.53E spec DAS sources
	 * @return
	 */
	@Deprecated
	public GROUP createGroup(String id, String label, String type){
		GROUP group=factory.createGROUP();
		group.setId(id);
		group.setLabel(label);
		group.setType(type);
		return group;
	}

	public FEATURE createFeature(String id, String start, String stop, String orientation, String phase, String score, METHOD method, TYPE type){
		FEATURE feature = factory.createFEATURE();
		feature.setId(id);
		feature.setSTART(new BigInteger(start));
		feature.setEND(new BigInteger(stop));
		feature.setMETHOD(method);
		feature.setORIENTATION(orientation);
		feature.setPHASE(phase);
		feature.setSCORE(score);
		feature.setTYPE(type);
		
		return feature;
		
	}
	/**
	 * Create feature with Parent and Part references 1.6 spec style
	 * @param id
	 * @param start
	 * @param stop
	 * @param orientation
	 * @param phase
	 * @param score
	 * @param method
	 * @param type
	 * @param parentFeatureIds
	 * @param partFeatureIds
	 * @return
	 */
	public FEATURE createFeatureWithParentsAndParts(String id, String start, String stop, String orientation, String phase, String score, METHOD method, TYPE type, List<String> parentFeatureIds, List <String> partFeatureIds){
		FEATURE feature= this.createFeature(id, start, stop, orientation, phase, score, method, type);
		if(parentFeatureIds!=null){
		for(String parentId:parentFeatureIds){
			feature.getPARENT().add(this.createParent(parentId));
		}
		}
		if(partFeatureIds!=null){
		for(String partId:partFeatureIds){
			feature.getPART().add(this.createPart(partId));
		}
		}
		return feature;
	}
	
	public FEATURE createFeatureWithParents(String id, String start, String stop, String orientation, String phase, String score, METHOD method, TYPE type, List<String> parentFeatureIds){
		FEATURE feature=this.createFeatureWithParentsAndParts(id, start, stop, orientation, phase, score, method, type, parentFeatureIds, null);
		return feature;
	}
	
	public FEATURE createFeatureWithParts(String id, String start, String stop, String orientation, String phase, String score, METHOD method, TYPE type, List<String> partFeatureIds){
		FEATURE feature=this.createFeatureWithParentsAndParts(id, start, stop, orientation, phase, score, method, type, null, partFeatureIds);
		return feature;
	}
	/**
	 * Deprecated but here if you need 1.5 compliant Features
	 * @param id
	 * @param start
	 * @param stop
	 * @param orientation
	 * @param phase
	 * @param score
	 * @param method
	 * @param type
	 * @return
	 */
	@Deprecated
	public FEATURE createFeature(String id, String start, String stop, String orientation, String phase, String score, METHOD method, TYPE type, List<GROUP>  groups){
		FEATURE feature= this.createFeature(id, start, stop, orientation, phase, score, method, type);
		feature.getGROUP().addAll(groups);
		return feature;
	}
	
	public TYPE createType(String id, String content, String category, String cvId){
		TYPE type = factory.createTYPE();
		type.setContent(content);
		type.setId(id);
		type.setCategory(category);
		type.setCvId(cvId);
		return type;
	}
	
	public METHOD createMethod(String id, String content, String cvId){
		METHOD method = factory.createMETHOD();
		method.setContent(content);
		method.setId(id);
		method.setCvId(cvId);
		return method;
	}
	
	public SEGMENT createSegment(String id, String version, String start, String stop, List<FEATURE> features){
		SEGMENT segment = factory.createSEGMENT();
		segment.setId(id);
		segment.setVersion(version);
		segment.setStart(start);
		segment.setStop(stop);
		segment.getFEATURE().addAll(features);
		return segment;
	}
	
	/**
	 * convenience method as in most cases a request is only made for a single segment
	 * @param segment
	 * @return
	 */
//	public DASGFFBuilder createDASGFF(SEGMENT segment){
//		List<SEGMENT> segments=new ArrayList<SEGMENT>();
//		DASGFFBuilder dgff = this.createDASGFF(segments);
//		return dgff;
//	}
	
        /**
         * 
         * @param segments either segment,unknownsegment, errorsegment all are subclasses of Object
         * @return 
         */
        public DASGFF createDASGFF(List<Object> segments, String href, BigInteger total){
		DASGFF dasgff = factory.createDASGFF();
		GFF gff = factory.createGFF();
                gff.setHref(href);
                gff.setTotal(total);
		for(Object segment: segments){
			gff.getSEGMENTOrERRORSEGMENTOrUNKNOWNSEGMENT().add(segment);
		}
		dasgff.setGFF(gff);
		return dasgff;
		
	}
        
        public DASGFF createDASGFF(List<Object> segments, String href){
		return this.createDASGFF(segments, href, null);
		
	}
	
}
